package com.lightsh.common;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lightsh.handwritten.R;
import com.lightsh.list.AbstractListItem;
import com.lightsh.list.AbstractListItemView;
import com.lightsh.list.OnItemSelectListener;
import com.lightsh.utility.Utility;

public class PaintItemView extends AbstractListItemView implements View.OnClickListener{

	private TextView mTextView;
	private ImageView mSelectView;
	private ImageView mPaintImage;
	private static boolean sInitWidth = false;
	private static int mPaintWidth;
	private static int mPaintHeight;
	
	public PaintItemView(Context context) {
		super(context);
	}
	public PaintItemView(Context context, OnItemSelectListener listener){
		super(context, listener);
	}

	@Override
	public void initView() {
		// 初始化view
		View view = LayoutInflater.from(this.mContext).inflate(R.layout.paint_item, null);
		mTextView = (TextView) view.findViewById(R.id.timeText);
		mSelectView = (ImageView)view.findViewById(R.id.rightImage);
		mPaintImage = (ImageView) view.findViewById(R.id.paintImage);
		// 如果允许选中，需要设置子view的onclicklistener
		if (mItemSelectListener != null) {
			view.findViewById(R.id.rightLayout).setOnClickListener(this);
		}
		addView(view);		
	}
	public static void initSize(Activity a, int columnSize){
		if(!sInitWidth){
			mPaintHeight = a.getResources().getDimensionPixelSize(R.dimen.paint_item_height);			
			int activityWidth = a.getWindowManager().getDefaultDisplay().getWidth();		
			mPaintWidth = activityWidth / columnSize;
			sInitWidth = true;
		}
	}

	@Override
	public void updateView(AbstractListItem item, int position) {
		if(!(item instanceof PaintItem)){
			throw new IllegalArgumentException("item must instance of PaintItem");
		}
		final PaintItem paintItem = (PaintItem) item;
		// 初始化index和selected
		mIndex = position;
		mbSelected = paintItem.isSelected();
		
		// 更新控件状态
		updateSelectStatus();
		mTextView.setText(Utility.formatTime(paintItem.getLastlyTime()));
		// 显示文件
		mPaintImage.setImageBitmap(Utility.paintToBitmap(mContext, paintItem, mPaintWidth, mPaintHeight));
	}

	private void updateSelectStatus(){
		if(mbSelected){
			mSelectView.setImageResource(R.drawable.selection_icon);
		}else{
			mSelectView.setImageResource(R.drawable.select_icon);
		}
	}
	
	@Override
	public void onClick(View v) {
		// 这里做点击的判断和处理
		if(v.getId() == R.id.rightLayout){
			// 可以增加是否允许点击的判断
			mbSelected = !mbSelected;
			// 更新ui
			updateSelectStatus();
			notifySelectedChange();
		}
		
	}
}
